﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using LGE;

namespace GGJ2014
{
    public class PlayScreen : GameScreen
    {
        //UserInterface ui;
        public Map map;
        public Player player;

        public List<GameObject> GameObjects = new List<GameObject>();

        public PlayScreen()
            : base()
        {
        }

        public override void LoadContent()
        {
            //ui = new UserInterface(this);
            player = new Player(this);
            LoadLevel("Level\\map.tmx");
            GetMapObjects();
            Audio.PlayBGM("level");
        }

        public void LoadLevel(String name)
        {
            map = Map.Load(Path.Combine(Engine.game.Content.RootDirectory, name), Engine.game.Content);
        }

        private void GetMapObjects()
        {
            /*foreach (MapObject mObj in map.ObjectGroups["obj"].Objects.Values)
            {
                if (mObj.Name.Contains("player"))
                    player.position = mObj.Position;
                else if (mObj.Name.Contains("coin"))
                    GameObjects.Add(new Coin(this, mObj.Position));
            }
            foreach (MapObject mObj in map.ObjectGroups["keys"].Objects.Values)
                if (mObj.Name.Contains("key"))
                    GameObjects.Add(new Key(this, mObj.Position));
            foreach (MapObject mObj in map.ObjectGroups["doors"].Objects.Values)
                if (mObj.Name.Contains("door"))
                    GameObjects.Add(new Door(this, mObj.Position, mObj.Name.Contains("v")));
            foreach (MapObject mObj in map.ObjectGroups["arrows"].Objects.Values)
                if (mObj.Name.Contains("arrow"))
                    GameObjects.Add(new ArrowTrap(this, mObj.Position, mObj.Name.Contains("down") ? Direction.Down :
                                                                       mObj.Name.Contains("up") ? Direction.Up :
                                                                       mObj.Name.Contains("left") ? Direction.Left : Direction.Right));
             */
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.Pressed(KeyMap.DebugMode)) GGJ2014.isDebugMode = !GGJ2014.isDebugMode;
            player.Update(gameTime);
            UpdateList(gameTime, GameObjects);
        }

        protected void UpdateList(GameTime gameTime, IEnumerable<GameObject> ieList)
        {
            List<GameObject> list = (List<GameObject>)ieList;
            for (int i = 0; i < list.Count; ++i)
            {
                list[i].Update(gameTime);
                if (!list[i].alive) list.RemoveAt(i);
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            MapManager.Draw(spriteBatch, map, player.CameraOffset);
            foreach (GameObject go in GameObjects) go.Draw(spriteBatch);
            player.Draw(spriteBatch);
            //MapManager.DrawFG(spriteBatch, map, player.CameraOffset);

            // UI
            for (int i = 0; i < 5; ++i)
                spriteBatch.Draw(player.avatars[i], new Vector2(10 + i * 80, 10), Color.White);
            spriteBatch.Draw(GameObject.tile, new Vector2(50 + (int)player.c * 80, 50), Color.White);
        }
    }
}
